/* Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com */

/*
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef TEST_UTILS_H_
#define TEST_UTILS_H_


#include <time.h>

#include "testcase.h"
#include "../src/utils.h"

class test_utils : public testcase {
public:
	test_utils() :
		testcase() {};
	virtual ~test_utils() {};

	inline virtual void run_tests() {
		cout<<"\n*********************************"<<endl;
		cout<<"*"<<endl;
		cout<<"* Running Time Tests"<<endl;
		cout<<"*"<<endl;
		cout<<"*********************************"<<endl;
		testSleep();
		testTimer();
		testPow2();
	}

	inline void testSleep() {
		
		cout<<"testing sleep ...."<<endl;
		time_t now = time(NULL);
		tri::utils::sleep(2000);
		assert( ( time(NULL) - now ) == 2);

	}
	
	
	inline void testTimer(){
		
		cout<<"testing timer clock mode...."<<endl;
		
		tri::utils::timer _timer;
		_timer.setMode(CLOCK_MODE);
		assert(!_timer.isRunning());
		assert(_timer.getElapsedTime()==0);
		assert(_timer.getProgress() == 1.0);
		
		assert(_timer.start());
		assert(_timer.isRunning());
		
		_timer.stop();
		
		assert(!_timer.isRunning());
		assert(_timer.getElapsedTime()==0);
		assert(_timer.getProgress() == 1.0);
		
		cout<<"testing timer defualt timer mode...."<<endl;
		_timer.setMode(TIMER_MODE);
		
		assert(!_timer.isRunning());
		assert(_timer.getElapsedTime()==0);
		assert(_timer.getProgress() == 1.0);
		
		assert(_timer.start());
		assert(_timer.isRunning());
		
		tri::utils::sleep(600);
		
		assert(_timer.isRunning());
		assert(_timer.getProgress()>0.5);
		assert(_timer.getElapsedTime()>0.5);
		
		tri::utils::sleep(600);
		
		assert(_timer.isRunning());
		assert(_timer.getProgress()==1.0);
		assert(_timer.getElapsedTime()==1.0);
		
		_timer.stop();
		
		assert(!_timer.isRunning());
		assert(_timer.getElapsedTime()==0);
		assert(_timer.getProgress() == 1.0);
		
		cout<<"testing timer timer mode...."<<endl;
		_timer.setDurationTime(2.0);
		
		assert(_timer.start());
		assert(_timer.isRunning());
		
		tri::utils::sleep(2050);
		
		assert(_timer.isRunning());
		assert(_timer.getProgress()==1.0);
		assert(_timer.getElapsedTime()== 2.0);
		
		_timer.stop();
		
		assert(!_timer.isRunning());
		assert(_timer.getElapsedTime()==0);
		assert(_timer.getProgress() == 1.0);
		
		
		
	}
	
	inline void testPow2(){
			cout<<"testing power of 2...."<<endl;
			assert(tri::utils::is_pow2(2));
			assert(tri::utils::is_pow2(4));
			assert(tri::utils::is_pow2(8));
			assert(tri::utils::is_pow2(16));
			
			assert(!tri::utils::is_pow2(0));
			assert(!tri::utils::is_pow2(1));
			assert(!tri::utils::is_pow2(5));
			assert(!tri::utils::is_pow2(100));
			
			assert(tri::utils::next_pow2(1)==2);
			assert(tri::utils::next_pow2(3)==4);
			assert(tri::utils::next_pow2(100)==128);
			assert(tri::utils::next_pow2(20)==32);
			assert(tri::utils::next_pow2(800)==1024);

	}
};

#endif /*TEST_ANIMATION_H_*/
